package jh.com.jhappbf.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author Administrator
 */
public class SQLiteDataBaseUtil extends SQLiteOpenHelper {

    private static final int mVersion = 1;
    private static SQLiteDatabase mDatabase = null;

    private SQLiteDataBaseUtil(Context context, String name,
                               CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    private SQLiteDataBaseUtil(Context context, String name, int version) {
        this(context, name, null, version);
    }

    private SQLiteDataBaseUtil(Context context, String name) {
        this(context, name, mVersion);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            /*
             * db.execSQL("create table  if not exists uploadManager ("+
             * " id varchar(255),Platenumber varchar(255), Cargoname varchar(255), Gross float,Tare float, Net float,Consignor varchar(255),"
             * +
             * "Consignee varchar(255),Specification varchar(255),Price float,reducationImpurity float,"
             * +
             * "reducationWater float,unloadWeight float,confirmPeople varchar(255),imgPath varchar(255),isUpload integer,sDate varchar(255),"
             * +
             * "sTime varchar(255),uploadTime varchar(255),type varchar(255), flag integer,address varchar(255) )"
             * ); db.execSQL(
             * "create table if not exists addressManager(id integer primary key autoincrement ,address varchar(255))"
             * ); db.execSQL(
             * "create table if not exists confirmManager(id integer primary key autoincrement,cardNum varchar(255),plateNum varchar(255),"
             * +
             * "spec varchar(255),sender varchar(255),receiver varchar(255),deduction float, moistrue float,cargoname varchar(255),address varchar(255), operator varchar(255),sDate varchar(255))"
             * ); db.execSQL(
             * "create table if not exists userManager(id integer primary key autoincrement,userAccount varchar(255), userName varchar(255),pwd varchar(255))"
             * ); db.execSQL(
             * "create table if not exists cargoNameManager(id integer primary key autoincrement,cargoName varchar(255))"
             * ); db.execSQL(
             * "create table if not exists specManager(id integer primary key autoincrement,spec varchar(255))"
             * );
             */
            db.execSQL("create table if not exists saverFileds(id integer primary key autoincrement,field nvarchar(50), alias nvarchar(50))");
            db.execSQL("create table if not exists areas(areaName varchar(255),clickAccount integer)");
            db.execSQL("create table if not exists addressManager(id integer primary key autoincrement ,address varchar(255))");
        } catch (Exception e) {
            String content = e.getMessage();
            content.length();
        }
    }

    public synchronized static SQLiteDatabase getDatabase(Context context) {
        if (mDatabase == null) {
            String path = context.getFilesDir() + java.io.File.separator + "data.db";
            SQLiteDataBaseUtil util = new SQLiteDataBaseUtil(context, path);
            mDatabase = util.getWritableDatabase();
        }
        return mDatabase;
    }

    /**
     * 关闭数据库
     */
    public static void closeData() {
        if (mDatabase != null) {
            mDatabase.close();
        }

        mDatabase = null;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}
